package com.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RequestMapping("/load")
@Controller
public class UploadController {

 //单文件上传
    @RequestMapping(value = "/load1",method = RequestMethod.POST)
    @ResponseBody
    public void up1(@RequestParam("username") String name, @RequestParam("load") MultipartFile uploadFile) throws IOException {
        //这里的 MultipartFile 的名字必须是 uploadFile，是别的接收不到
        System.out.println(name);
//        System.out.println(uploadFile);
        //获取文件名字
        String originalFilename = uploadFile.getOriginalFilename();
        uploadFile.transferTo(new File("D:\\Java\\"+originalFilename));
    }

    //多文件上传  用数组接收
    @RequestMapping(value = "/load2",method = RequestMethod.POST)
    @ResponseBody
    public void up2(@RequestParam("username") String name, @RequestParam("load") MultipartFile[] uploadFile) throws IOException {
        //这里的 MultipartFile 的名字必须是 uploadFile，是别的接收不到
        System.out.println(name);
        for(MultipartFile m : uploadFile){
            m.transferTo(new File("D:\\Java\\"+m.getOriginalFilename()));
        }



    }
}
